package com.maikang.exammall.util;

import com.maikang.exammall.entity.User;
import com.maikang.exammall.entity.constant.TokenConstant;
import com.maikang.exammall.service.RedisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

@Log4j2
@Component
@RequiredArgsConstructor
public class TokenUtil {
    final private RedisService redisService;
    // 设置 userIdToken.1232: token
    // 设置 json.token.xxx: {user_id: id, mobile: mobile}
    // TODO change back the expiretime to 24 hrs
    public void resetToken(String token, User user) {
        final String userIdTokenKey = TokenConstant.getUserId_TokenKeyFromUserId(user.getId());
        log.info("reseting token: {} for user {} with userIdTokenKey: {}", token, user, userIdTokenKey);
        redisService.set(userIdTokenKey, token, 3 * 24 * 3600L);
        final String tokenUserInfoKey = TokenConstant.getToken_userInfoKeyFromToken(token);
        redisService.set(tokenUserInfoKey, user, 3 * 24 * 3600L);
    }
}
